using System;
using System.Collections.Generic;
using System.Text;
using Sedna.API.Enums;
using Sedna.Core.Persistence;
using Sedna.Domain;
using Sedna.Domain.Enums;
using NHibernate;
using Nullables;

namespace Sedna.API.Filter
{
    [Serializable]
    public class BusinessHistoryFilter : AbstractFilterState, IHqlQueryFilterState
    {
        private string receiptNumber;
        private string receiptFrom;
        private string receiptTo;
        private string vaultCode;
        private NullableDateTime issueDateFrom;
        private NullableDateTime issueDateTo;
        private NullableDateTime dateFrom;
        private NullableDateTime dateTo;
        private string locationCode;
        private string customerCode;
        private string refNumber;

        #region Properties

        public string ReceiptNumber
        {
            get { return receiptNumber; }
            set { receiptNumber = value; }
        }

        public string ReceiptFrom
        {
            get { return receiptFrom; }
            set { receiptFrom = value; }
        }

        public string ReceiptTo
        {
            get { return receiptTo; }
            set { receiptTo = value; }
        }

        public string VaultCode
        {
            get { return vaultCode; }
            set { vaultCode = value; }
        }

        public NullableDateTime IssueDateFrom
        {
            get { return issueDateFrom; }
            set { issueDateFrom = value; }
        }

        public NullableDateTime IssueDateTo
        {
            get { return issueDateTo; }
            set { issueDateTo = value; }
        }

        public NullableDateTime DateFrom
        {
            get { return dateFrom; }
            set { dateFrom = value; }
        }

        public NullableDateTime DateTo
        {
            get { return dateTo; }
            set { dateTo = value; }
        }

        public string LocationCode
        {
            get { return locationCode; }
            set { locationCode = value; }
        }

        public string CustomerCode
        {
            get { return customerCode; }
            set { customerCode = value; }
        }

        public string RefNumber
        {
            get { return refNumber; }
            set { refNumber = value; }
        }

        #endregion

        public IQuery ToHqlQuery(ISession session)
        {
            HqlQueryBuilder builder = new HqlQueryBuilder();
            builder.Select.Add("history");
            builder.From.Add("WhsReceiptOwnershipHistory history");

            builder.Like("history.WhsReceipt.prefixedNumber", receiptNumber);
            builder.Between("history.WhsReceipt.prefixedNumber", receiptFrom, receiptTo);
            builder.Like("history.WhsReceipt.location.Warehouse.Code", vaultCode);
            builder.Like("history.WhsReceipt.location.Code", locationCode);
            builder.Like("history.WhsReceipt.customer.Code", customerCode);
            builder.Like("history.WhsReceipt.ReferenceNumber", refNumber);

            builder.Between("history.WhsReceipt.IssueDate", issueDateFrom, issueDateTo);
            builder.Ge("history.Fd", dateFrom);
            builder.Le("history.Td", dateTo);

            return builder.ToQuery(session);
        }

    }
}
